var has_input_DR = false;
var has_input_CR = false;
function check_d_value()
{
        var bool = true;
        var accountId = document.getElementById('d_accountId').value;
        var DRamount = document.getElementById('d_DRamount').value;
        var CRamount = document.getElementById('d_CRamount').value;
        var description = document.getElementById('d_description').value;
        
        if(accountId==""||(DRamount==""&&CRamount=="")||description=="")
        {               
                bool = false;
        }        
        if(CRamount!=""&&DRamount!="")
        {
                document.getElementById('d_DRamount').value = "";
                document.getElementById('d_CRamount').value = "";
                bool = false;
        } 
        if(has_input_DR&&CRamount!="")
        {
                document.getElementById('d_CRamount').value = "";
                bool = false;
        }
        if(has_input_CR&&DRamount!="")
        {
                document.getElementById('d_DRamount').value = "";
                bool = false;
        }
        return bool;
}
function check_g_value()
{
        var bool = true;
        var accountId = document.getElementById('g_accountId').value;
        var DRamount = document.getElementById('g_DRamount').value;
        var CRamount = document.getElementById('g_CRamount').value;
        var description = document.getElementById('g_description').value;
        
        if(accountId==""||(DRamount==""&&CRamount=="")||description=="")
        { 
                bool = false;
        }
        if(CRamount!=""&&DRamount!="")
        {            
                document.getElementById('g_DRamount').value = "";
                document.getElementById('g_CRamount').value = "";
                bool = false;
        }  
        return bool;
}
function add_d_row()
{
        if(check_d_value())
        {
                var dl = document.createElement('dl');
                dl.setAttribute('class','fin-title fin-form2'); 
                dl.setAttribute('className','fin-title fin-form2'); 
                
                var dd = document.createElement('dd');
                var accountId = document.getElementById('d_accountId').value;
                dd.innerHTML='<input type="hidden" name="d_accountId" value="'+accountId+'"/><a href="/bin/finance.py/show_sub_account_details?sl={{info_sl}}&a='+accountId+'">'+accountId+'</a>';
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var DRamount = document.getElementById('d_DRamount').value;
                var DRamount_text=DRamount;
                if(DRamount==''){
                        DRamount_text='&nbsp;';
                }
                dd.innerHTML='<input type="hidden" name="d_DRamount" value="'+DRamount+'"/>'+DRamount_text;
                dl.appendChild(dd);
                if(DRamount != "")
                {
                        has_input_DR = true;
                }
                
                var dd = document.createElement('dd');
                var CRamount = document.getElementById('d_CRamount').value;
                var CRamount_text=CRamount;
                if(CRamount==''){
                        CRamount_text='&nbsp;';
                }
                dd.innerHTML='<input type="hidden" name="d_CRamount" value="'+CRamount+'"/>'+CRamount_text;
                dl.appendChild(dd);
                if(CRamount != "")
                {
                        has_input_CR = true;
                }
                
                var dt = document.createElement('dt');
                var description = document.getElementById('d_description').value;
                dt.innerHTML='<input type="hidden" name="d_description" value="'+description+'"/>'+description;
                dl.appendChild(dt); 
                
                var obj = document.getElementById("sub_journal_list");                
                obj.appendChild(dl);            
    
                sum_d_to_g(description);
                clean_up('d');                
        }
}
function add_g_row()
{
		if(check_g_value())
		{  
				var dl = document.createElement('dl');
				dl.setAttribute('class','fin-title fin-form2'); 
				dl.setAttribute('className','fin-title fin-form2'); 
				
				var dd = document.createElement('dd');
				var accountId = document.getElementById('g_accountId').value;
				dd.innerHTML='<input type="hidden" name="g_accountId" value="'+accountId+'"/><a href="/bin/finance.py/show_account_details?a='+accountId+'">'+accountId+'</a>';
				dl.appendChild(dd);
				
				var dd = document.createElement('dd');
				var DRamount = document.getElementById('g_DRamount').value;
				var DRamount_text = DRamount;
				if(DRamount==''){
						DRamount_text='&nbsp;';
				}
				dd.innerHTML='<input type="hidden" name="g_DRamount" value="'+DRamount+'"/>'+DRamount_text;
				dl.appendChild(dd);
				
				var dd = document.createElement('dd');
				var CRamount = document.getElementById('g_CRamount').value;
				var CRamount_text = CRamount;
				if(CRamount==''){
						CRamount_text='&nbsp;';
				}
				dd.innerHTML='<input type="hidden" name="g_CRamount" value="'+CRamount+'"/>'+CRamount_text;     
				dl.appendChild(dd); 
				
				var dt = document.createElement('dt');
				var description = document.getElementById('g_description').value;
				dt.innerHTML='<input type="hidden" name="g_description" value="'+description+'"/>'+description;
				dl.appendChild(dt); 
				
				var obj = document.getElementById("general_journal_list");                
				obj.appendChild(dl); 
		
				clean_up('g');
				disabled_obj('g');
		}
}
function sum_d_to_g(description)
{
        var total_d_DR = 0;
        var total_d_CR = 0;
        
        var d_DRamount = document.getElementsByName("d_DRamount");
        var d_CRamount = document.getElementsByName("d_CRamount");
        
        for(m = 0; m < d_DRamount.length; m++) {
                if(d_DRamount[m].value==""){
                        total_d_DR += 0;
                }
                else{
                        total_d_DR += parseFloat(d_DRamount[m].value);
                }
        }
        
        for(n = 0; n < d_CRamount.length; n++){
                if(d_CRamount[n].value==""){
                        total_d_CR += 0;
                }
                else{
                        total_d_CR += parseFloat(d_CRamount[n].value);
                }
        }
        
        if(total_d_DR==0)
        {
                document.getElementById('td_g_CR').innerHTML = '<input type="hidden" id="g_CRamount_set" name="g_CRamount" value="'+total_d_CR.toString()+'"/>'+total_d_CR.toString();
                document.getElementById('td_g_DR').innerHTML = '<input type="hidden" id="g_DRamount_set" name="g_DRamount" value=""/>&nbsp;';
        }
        else
        {
                document.getElementById('td_g_CR').innerHTML = '<input type="hidden" id="g_CRamount_set" name="g_CRamount" value=""/>&nbsp;';
                document.getElementById('td_g_DR').innerHTML = '<input type="hidden" id="g_DRamount_set" name="g_DRamount" value="'+total_d_DR.toString()+'"/>'+total_d_DR.toString();
        }
		document.getElementById('td_g_description').innerHTML = '<input type="text" name="g_description" value="'+description+'"/>';
}
function check_balance()
{
        var total_g_DR = 0;
        var total_g_CR = 0;
        
        var g_DRamount = document.getElementsByName("g_DRamount");
        var g_CRamount = document.getElementsByName("g_CRamount");
        
        for(i = 0; i < g_DRamount.length; i++) {
                if(g_DRamount[i].value==""){total_g_DR += 0;}
                else{total_g_DR += parseFloat(g_DRamount[i].value);}
        }
        
        for(j = 0; j < g_CRamount.length; j++){
                if(g_CRamount[j].value==""){total_g_CR += 0;}
                else{total_g_CR += parseFloat(g_CRamount[j].value);}
        }
        
        if(total_g_DR == total_g_CR&&total_g_DR!=0)
        {
                return true;
        }
        else
        {                
                return false;
        }
}
function clean_up(str)  
{   
        document.getElementById(str+'_accountId').value='';
        document.getElementById(str+'_CRamount').value='';
        document.getElementById(str+'_DRamount').value='';
        document.getElementById(str+'_description').value='';        
}
function check_and_submit(id)
{
        var form = document.getElementById(id);
        var g_description = document.getElementsByName("g_description");
        for(i=0;i<g_description.length;i++){
                if(g_description[i].value==""){
                        alert("please enter description!");
                        return;
                }                
        }
        if(check_balance()){
                form.submit();
        }else{
                alert('Not balance!');
                return;
        }
}
function disabled_obj(flag){
		var obj1 = document.getElementById(flag+'_DRamount');
		var obj2 = document.getElementById(flag+'_CRamount');
		if(obj1.value.trim().length==0 && obj2.value.trim().length==0){
				//obj1.disabled=null;
				//obj2.disabled=null;
				obj1.style.display='block';
				obj2.style.display='block';
				return;
		}
		if(obj1.value.trim().length>0){                
				obj2.style.display='none';
				//obj2.disabled='disabled';
				return;
		}
		if(obj2.value.trim().length>0){
				obj1.style.display='none';               
				//obj1.disabled='disabled';
				return;
		}   
}
